//
//  PostsAFN.m
//  丁诚昊的外包项目(2016_V7宝)
//
//  Created by DimChum_Haul on 16/3/27.
//  Copyright © 2016年 沙罗双树831. All rights reserved.
//

#import "PostsAFN.h"
// ---------------------- 发送网络请求 数据转换 ----------------------
#import "AFNetworking.h"
#import "SVProgressHUD.h"

NSUInteger const PORT_LBDS   = 8090;
NSString* const Domain_LBDS = @"http://www.lbds123.com";

float const EtherNetReuqestTimeOutValue = 3.0f;
float const globalDurationDisplayingTime = 0.33f;

@implementation PostsAFN

+ (NSString *)CutUpUrl:(NSString *)apiName
{
    __block NSDictionary* APIList = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        APIList = @{
        @"API_FlowList"   : @"VB/gw/spl/gm/commodityCx/shopHomepageGuessLikeApplyApp.action",
        @"API_Pay4Credits": @"VB/trade/unionpay/app/IntegralChangePa/integralChangePayApplyApp.action",
        @"APi_User_Login" : @"VB/yh/dl/memberLogin/memberLoginApplyApp.action",
        @"API_Register"   : @"VB/fs/dx/MessagesFs/sendMessagesVerifyApplyApp.action",
        @"API_VerifyMsg"  : @"VB/yh/cz/memberEnroll/memberEnrollApplyApp.action",
        @"API_ChangePWD"  : @"VB/yh/cz/memberEnroll/memberUpdatePwdApplyApp.action"
        };
    });
    NSString* keyValue = (NSString*)[APIList valueForKey:apiName];
    NSString* finalString = [NSString stringWithFormat:@"%@:%@/%@",Domain_LBDS,@(PORT_LBDS),keyValue];
    NSLog(@"--->%@",finalString);
    return finalString;
}

#pragma mark - AFN 4 HTTP & HTTPS & Availlibility & Multitask
// Mark :~ 在block外部发送请求体 这样就无所谓你使用的是哪种类型的第三方网络框架了

BlockNettingInjection2AFN _blockCopyStack;
+ (void)commonSessionRequestBy_AFN:(NSDate *(^)(id))successblock
                         urlString:(NSString *)API_String
                            params:(NSDictionary *)paramsDict
{    
    /** 这是AFN必须设置的参数否则会报错 */
    /// HTTP解析策略 [读取]
    NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFHTTPSessionManager* SessionManager = [[AFHTTPSessionManager alloc]
                                            initWithSessionConfiguration:config];
    
    AFHTTPResponseSerializer* responseSerializer = [[AFHTTPResponseSerializer alloc]init];
    responseSerializer.acceptableContentTypes = [NSMutableSet setWithObjects:@"text/plain",nil];
    SessionManager.responseSerializer = responseSerializer;
    /// HTTP请求策略 [写入]
    AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer];
    requestSerializer.timeoutInterval = 5.0f;
    requestSerializer.stringEncoding = NSUTF8StringEncoding;
    SessionManager.requestSerializer = requestSerializer;
    
    _blockCopyStack = [successblock copy];
    [SessionManager POST:[PostsAFN CutUpUrl:API_String]
              parameters:paramsDict
                progress:nil
                 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(_blockCopyStack) {
            _blockCopyStack(responseObject);
            [SVProgressHUD showSuccessWithStatus:@"成功获取HTTP数据"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.33f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD popActivity];
            });
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"---->%@",error);
        [SVProgressHUD showErrorWithStatus:@"HTTP请求失败"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.67f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD popActivity];
        });
    }];
}

+ (char*) NSString2CString:(NSString*)oriStr {
    return (char*)[oriStr UTF8String];
}
+ (NSString*) ChineseFromJSONDict:(NSDictionary *)dict{
    NSData* data = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:nil];
    NSString* resultString = [[NSString alloc]initWithData:data
                                                  encoding:NSUTF8StringEncoding];
    return resultString;
}
@end
